package org.zch.common;


import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.http.HttpStatus;

/**
 * ApiResult 统一返回值封装
 *
 * @author 刘新
 * @date 2023/12/28 10:17 PM
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ApiResult<T> {
    @ApiModelProperty(value = "200 成功, 400未登录,401账户不存在")
    private int status;
    @ApiModelProperty(value = "错误信息")
    private String errorMsg;
    @ApiModelProperty(value = "返回数据")
    private T data;
    @ApiModelProperty(value = "是否自定义及解析提示")
    private Integer needs;

    public ApiResult() {
    }

    private ApiResult(int status, T data, String errorMsg) {

        this.status = status;
        this.data = data;
        this.errorMsg = errorMsg;
    }

    private ApiResult(int status, T data, String errorMsg, Integer needs) {
        this.status = status;
        this.data = data;
        this.errorMsg = errorMsg;
        this.needs = needs;
    }

    private ApiResult(int status, String errorMsg) {

        this.status = status;
        this.errorMsg = errorMsg;
    }


    /**
     * 构造方法
     *
     * @param <T>
     * @return
     */
    public static <T> ApiResult<T> ok() {
        return new ApiResult<>(HttpStatus.OK.value(), "");
    }

    public static <T> ApiResult<T> ok(T data) {
        return new ApiResult<>(HttpStatus.OK.value(), data, null);
    }

    public static <T> ApiResult<T> error(HttpStatus status, String errorMsg) {
        return new ApiResult<>(status.value(), null, errorMsg);
    }

    public static <T> ApiResult<T> error(int status, String errorMsg) {
        return new ApiResult<>(status, null, errorMsg);
    }

    public static <T> ApiResult<T> error(int status, String errorMsg, T data, Integer needs) {
        return new ApiResult<>(status, data, errorMsg,needs);
    }

    public static <T> ApiResult<T> error(int status, String errorMsg, T data) {
        return new ApiResult<>(status, data, errorMsg);
    }

    public int getStatus() {
        return status;
    }

    public ApiResult<T> setStatus(int status) {
        this.status = status;
        return this;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public ApiResult<T> setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
        return this;
    }

    public T getData() {
        return data;
    }

    public ApiResult<T> setData(T data) {
        this.data = data;
        return this;
    }

    public Integer getNeeds() {
        return needs;
    }

    public void setNeeds(Integer needs) {
        this.needs = needs;
    }
}
